<?php
/**
 * This file contains a loggin mechanism
 *
 * PHP version 5
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @version   SVN: $Id: login.class.php 98 2010-07-11 12:33:40Z sruttloff@googlemail.com $
 * @link      phpmy
 */

/**
 * loggin class
 *
 * @category  PHP
 * @package   PHPMY
 * @author    Sven Ruttloff, www.sruttloff.de <service@sruttloff.de>
 * @copyright 2010 Sven Ruttloff, www.sruttloff.de
 * @license   http://www.sruttloff.de/cms/files/licence_en_lgpl.txt LGPL
 * @link      phpmy
 */
class login
{
    /**
     * @var object instance
     */
    protected static $INSTANCE;
    /**
     * instantiate essential objects and make them avail. as subobjects
     *
     * @return void
     */
    protected function __construct()
    {
    }
    /**
     * return a instance of object
     *
     * @return login instance
     */
    public static function getInstance()
    {
        if (!isset(self::$INSTANCE))
        {
            $c = __CLASS__;
            self::$INSTANCE = new $c ();
        }
        return self::$INSTANCE;
    }
    /**
     * check if user is logged in
     *
     * @return bool
     */
    public function checkLogin()
    {
        $sess = frontend::getInstance()->getSession();
        if (!is_object($sess))
            return false;
        return ($this->getUserId() ? true : false);
    }
    /**
     * login a user
     *
     * @param int   $int_userid   user id no
     * @param mixed $arr_userrole optional | array of user roles or single role name
     *
     * @return bool
     */
    public function doUserLogin($int_userid = 0, $arr_userrole = array())
    {
        if (is_null($int_userid) || !is_numeric($int_userid))
            return false;
        if (!is_array($arr_userrole))
            $arr_userrole = array ($arr_userrole);
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $tmp = md5($int_userid . serialize($arr_userrole));
        $sess->set($tmp . '_felogin_user_id', $int_userid);
        $sess->set($tmp . '_felogin_user_role', serialize($arr_userrole));
        $sess->set('crc_felogin_user_crc', $tmp);
    }
    /**
     * logout a user from session
     *
     * @return  bool
     */
    public function doUserLogoff()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $tmp = $sess->set('crc_felogin_user_crc');
        $sess->delete($tmp . '_felogin_user_id');
        $sess->delete($tmp . '_felogin_user_role');
        $sess->delete('crc_felogin_user_crc');
        return false;
    }
    /**
     * return user id of logged in user
     *
     * @return int user id
     */
    public function getUserId()
    {
        $sess = frontend::getInstance()->getSession();
        if (!is_object($sess))
            return false;
        $crc = $sess->get('crc_felogin_user_crc');
        return $sess->get($crc . '_felogin_user_id');
    }
    /**
     * return user role/s of logged in user
     *
     * @return array user role/s
     */
    public function getUserRole()
    {
        $frontend = frontend::getInstance();
        $sess = $frontend->getSession();
        if (!is_object($sess))
            return false;
        $crc = $sess->get('crc_felogin_user_crc');
        return unserialize($sess->get($crc . '_felogin_user_role'));
    }
}